<?php

/**********************************************************************
 *                                                                    *    
 * Copyright (c) 2009 Dmytro Bezhnar. All rights reserved.     			  *
 *                                                                    *
 **********************************************************************/

/**
 * Controller Ajax2
 *
 * @version $Id: Controller_ajax2.php,v 1.15 2008/09/10 13:35:16 master Exp $
 */

class Controller_ajax2 extends Controller
{
	/**
	 * Database connection
	 * @var Connection
	 */
    private $connection = null;	
	 
    public $cache_execute = false;
    
    /**
     * Array for JSON data response
     */
    protected $ajaxResp = array();
    /**
     * Is Json Style Output 
     */
    public $isJsonOutput = true;

	/**
	 * Основной метод управления работой скрипта, который запускается скразу после инициализации диалогов
	 *
	 */
    	
	function handleRequest()
    {
   		global $options;

   		# fix DataBase Charset
		$this->fixDBCharset();
   		
    	$this->setup_options();
    	
		$this->setup_params();
		
		if ($this->options['multilanguage']) {
    		$this->mLanguage();
		}
		
		if ($this->options['auth']) {
			$this->psp_auth= new Auth();
			$this->psp_auth->checkIsLogged();

			if ($this->prefix!='admin/') {
	    	 	$isLogged=$this->psp_auth->isLogged();
			} else {
	    	 	$isLogged=$_SESSION['admin_id'];
			}
    	}
    	 
		if ($this->cache_execute) {
			$cache = new CacheAjax();
			$cache->init(&$this);
		}        
    	 
    	 // if required, check for the logged user and redirect to login page
        if ($this->isSecure() && !$this->isLogged()) {
//----------------------------------------------------------------------------------------------------------------------
        }else{
		  $this->prepare();
        }
        
        if (!$this->silent) { 
        	$this->display();
        }
    }

    /**
     * Check if user login 
     * @return integer
     */
  	function isLogged() { return $_SESSION['person_id']; }
  
    /**
     * Возвращает название деректории для системы темплейтов при включоном foldertemplates
     * @return string
     */
  	    protected function getFolderTemplates() {
    	if ($this->options['foldertemplates']) {
    		return 'ajax/';
    	}
    	return '';
    }
    
    protected function display() {
        if ($this->isJsonOutput) {
			header('Content-Type: text/plain; charset=UTF-8');
        	print json_encode($this->ajaxResp);
        } else {
    		header('Content-Type: '.$this->content_type.'; charset='.$this->charset);
        	print $this->getTemplate();
        }
    }

    public function mLanguage() {
    	if ($_SESSION['lang_id'] == 0 or $_SESSION['lang_code'] == '') {
    		parent::mLanguage();
    	} else {
	    	$this->lang_id   = $_SESSION['lang_id'];
	    	$this->lang_code = $_SESSION['lang_code'];
	    	$this->setup_charset();
    	}
    }
    
    function fixDBCharset() {
    	if ($this->isJsonOutput) {
    		$this->RewriteManager->setCharset('utf8');
    	}
    }    
}
?>
